package com.bringway.global.util;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.regex.Matcher;  
import java.util.regex.Pattern;

import org.apache.commons.lang3.StringUtils;

public class NumberUtil {
	
	// 默认除法运算精度  
    private static final Integer DEF_DIV_SCALE = 2;  
    
	public static void main(String[] args) {  
		System.out.println(Double.parseDouble("4.01") - 2 );  
		System.out.println(Double.parseDouble("2.01") - 2);  
		System.out.println(Double.parseDouble("2.01") - Double.parseDouble("2.1"));  
		System.out.println(Double.parseDouble("2.01") - 2 >= Double.parseDouble("0.01"));
        System.out.println(add(0.06, 0.00));  
        System.out.println(sub(100.0, 2)); 
        System.out.println(isNumber("15135.0")); 
    }  
  
    public static double add(double v1, double v2) {// 加法  
        BigDecimal b1 = new BigDecimal(Double.toString(v1));  
        BigDecimal b2 = new BigDecimal(Double.toString(v2));  
        return b1.add(b2).doubleValue();  
    }  
  
    public static double sub(double v1, double v2) {// 减法  
        BigDecimal b1 = new BigDecimal(Double.toString(v1));  
        BigDecimal b2 = new BigDecimal(Double.toString(v2));  
        return b1.subtract(b2).doubleValue();  
    }  
  
    public static double mul(double v1, double v2) {// 乘法  
        BigDecimal b1 = new BigDecimal(Double.toString(v1));  
        BigDecimal b2 = new BigDecimal(Double.toString(v2));  
        return b1.multiply(b2).doubleValue();  
    }  
  
    public static double div(double v1, double v2) {// 除法  
        BigDecimal b1 = new BigDecimal(Double.toString(v1));  
        BigDecimal b2 = new BigDecimal(Double.toString(v2));  
        return b1.divide(b2, DEF_DIV_SCALE, BigDecimal.ROUND_HALF_UP).doubleValue();  
    }  
  
    public static double round(double v) {// 截取DEF_DIV_SCALE位  
        BigDecimal b = new BigDecimal(Double.toString(v));  
        BigDecimal one = new BigDecimal("1");  
        return b.divide(one, DEF_DIV_SCALE , BigDecimal.ROUND_HALF_UP).doubleValue();  
    }  
  
    public static String decimalFormat(String pattern, double value) {  
        return new DecimalFormat(pattern).format(value);  
    }  
  
    public static String decimalFormat(double value) {  
        return new DecimalFormat("0.00").format(value);  
    }  
  
    public static String decimalFormat(double value, String pattern) {  
        return new DecimalFormat(pattern).format(value);  
    }  
  
    public static String decimalBlankFormat(double value) {  
        return new DecimalFormat("0").format(value);  
    }  
  
    public static boolean isNumber(String value) { // 检查是否是数字  
    	if(StringUtils.isEmpty(value)){
    		return false;
    	}
        String patternStr = "^\\d+$";  
        Pattern p = Pattern.compile(patternStr, Pattern.CASE_INSENSITIVE); // 忽略大小写;  
        Matcher m = p.matcher(value);  
        return m.find();  
    }  
}
